package com.xian.tszs.meal.server;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import com.xian.tszs.meal.entity.TsAdmin;
import com.xian.tszs.meal.mapper.TsAdminMapper;


/***
 * 用户验证
 * @author 马瑞朝
 *
 */
@Component
public class AdminServiceDetail implements UserDetailsService {
	
	@Autowired
	private TsAdminMapper tsAdminMapper;
	
	@Override
	public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
		
		TsAdmin u = tsAdminMapper.findByAccount(name);
		
		if (u == null) {
			throw new AuthenticationCredentialsNotFoundException("authError");
		}
		List<GrantedAuthority> authorities = new ArrayList<>();
		
		authorities.add(new SimpleGrantedAuthority(u.getRole()));
		
		u.setAuthorities(authorities);
		
		return u;
	}
}
